/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import domen.User;
import ejb.UserFacade;
import java.net.UnknownHostException;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpSession;
import mail.MailServer;

/**
 *
 * @author Danilo
 */
@ManagedBean(name = "registermanagedbean")
@RequestScoped
public class RegisterManagedBean {

    private String firstname;
    private String sessionId;
    private String lastname;
    private Date dateOfBirth;
    private String password;
    private String email;
    private String username;
    private String repass;
    
    @EJB
    private UserFacade ufr;

    /** Creates a new instance of RegisterManagedBean */
    public RegisterManagedBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        sessionId = session.getId();
    }

    /**
     * @return the firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * @return the lastname
     */
    public String getLastname() {
        return lastname;
    }

    /**
     * @param lastname the lastname to set
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    /**
     * @return the dateOfBirth
     */
    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    /**
     * @param dateOfBirth the dateOfBirth to set
     */
    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    public void register() {
        if (!getPassword().equals(getRepass())) {
            FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage("Unete sifre se razlikuju. Molimo Vas pokusajte ponovo!"));
            password = "";
            repass = "";
        }
        try {
            String message = "";
            if (ufr.find(username) == null) {
                ufr.create(new User(username, false, email, firstname, lastname, password, false, sessionId));
                message = "Uspesno ste se registrovali. Da bi aktivirali vas nalog pratite link koji vam je poslat na email.";
                new MailServer().sendMail("danilopartalj@gmail.com", "latRodectuS", email, "Registration confirm", getMessage());
            } else if (!ufr.find(username).getActivated()) {
                User u = ufr.find(username);
                u.setSessionId(sessionId);
                ufr.edit(u);
                message = "Nalog vec postoji. Da bi aktivirali vas nalog pratite link koji vam je poslat na email.";
                new MailServer().sendMail("danilopartalj@gmail.com", "latRodectuS", email, "Registration confirm", getMessage());
            } else {
                message = "Korisnik sa korisnickim imenom: " + getUsername() + " je vec registrovan!";
            }
            FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_INFO, "", message));
        } catch (AddressException ex) {
            FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_WARN, "Greska pri registraciji", "Email adresa nije u odgovarajucem formatu!"));
        } catch (MessagingException ex) {
            FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Greska pri registraciji", "Doslo je do greske i poruka nije poslata!"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_INFO, "", ex.getMessage()));
        }
        username = "";
        password = "";
        repass = "";
        dateOfBirth = null;
        firstname = "";
        lastname = "";
        email = "";
    }

    private String getMessage() throws UnknownHostException {
        return "Korisnicko ime: "+username+"/nLozinka :"+password+"/nMolimo vas pratite ovaj link kako biste aktivirali vas nalog http://srbdroid.no-ip.info/faces/login.xhtml?id=" + sessionId;
    }
    

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the repass
     */
    public String getRepass() {
        return repass;
    }

    /**
     * @param repass the repass to set
     */
    public void setRepass(String repass) {
        this.repass = repass;
    }

}
